//app.js
App({
  onLaunch: function () {
    const that = this
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        traceUser: true,
      })
    }
    this.login()
  },
  onHide:function(){

  },
  setdata: function () {
    const that = this
    //第一次访问页面会调用函数，调用完存入缓存，减少调用次数
    wx.getStorage({
      key: 'weatherData',
      success(res) {
        // console.log(res)
        that.setData({
          weatherData: res.data
        })
      },
      fail: function (err) {
        console.log(err)
        that.setData({
          spinShow: true
        })
        that.getWeather()
      }
    })
    wx.getStorage({
      key: 'bady',
      success(res) {
        // console.log(res)
        that.setData({
          bady: res.data
        })
      },
      fail: function (err) {
        console.log(err)
        that.setData({
          spinShow: true
        })
        that.getbady()
      }
    })
    //定时器，实时检测数据，关掉spin
    setInterval(() => {
      if (this.data.weatherData != '' && this.data.bady != '') {
        that.setData({
          spinShow: false
        })
      }
    }, 300);
  },
  login: function () {
    const that = this
    return new Promise(function (resolve, reject) {
      const db = wx.cloud.database()
      that.globalData.spinShow  =  true
      wx.cloud.callFunction({
        name: 'login',
        complete: res => {
          // console.log(res.result.openid)
          db.collection('user').where({
              _openid: res.result.openid
            })
            .get({
              success: function (e) {
                // console.log(e)
                if (e.data.length == 0) {
                  // console.log(222)
                  that.globalData.openid =  res.result.openid
                } else {
                  that.globalData.userInfo= e.data[0],
                  that.globalData.isLogin =  true
                  resolve(e)
                }
              },
              fail: function (err) {
                reject(err)
              }
            })
        }
      })
    })
  },
  getUserProfile() {
    const that = this
    const db = wx.cloud.database()
    return new Promise((resolve,reject) => {
      if (!that.globalData.isLogin) {
        wx.getUserProfile({
          desc: '用于获取用户基本信息',
          success: function (res) {
            // console.log(res)
            that.globalData.userInfo = res.userInfo
            db.collection('user').add({
                data: {
                  avatarUrl: res.userInfo.avatarUrl,
                  city: res.userInfo.city,
                  country: res.userInfo.country,
                  gender: res.userInfo.gender,
                  language: res.userInfo.language,
                  nickName: res.userInfo.nickName,
                  province: res.userInfo.province,
                  openid: that.globalData.openid
                }
              })
              .then(res => {
                // console.log(res)
                that.globalData.isLogin = true
                resolve(res)
              })
          },
          fail(err) {
            console.log(err)
          }
        })
      }
    })
  },
  globalData:{
    isLogin:false
  }
})
